package com.example.costumview.customview2;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.w3c.dom.Text;

public class FlowLayout extends ViewGroup {

    private static final String TAG = "FlowLayout";
    public FlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int measureWidth = MeasureSpec.getSize(widthMeasureSpec);
        int measureHeight = MeasureSpec.getSize(heightMeasureSpec);
        int measureWidthMode = MeasureSpec.getMode(widthMeasureSpec);
        int measureHeightMode = MeasureSpec.getMode(heightMeasureSpec);

        int lineWidth = 0;//记录每一行的宽度
        int lineHeight = 0;//记录每一行的高度
        int height = 0; //记录整个FlowLayout所占高度
        int width = 0;//记录整个FlowLayout所占宽度

        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            measureChild(child, widthMeasureSpec, heightMeasureSpec);


            if (child.getLayoutParams() instanceof MarginLayoutParams) {
                MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
                int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
                int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;

                if (lineWidth + childWidth > measureWidth) {
                    //需要换行
                    width = Math.max(lineWidth, childWidth);
                    height += lineHeight;
                    //因为当前行放不下当前控件，而将此控件调到下一行，所以将此控件的高度和宽度初始化给lineHeight,lineWidth
                    lineWidth = childWidth;
                    lineHeight = childHeight;
                } else {
                    //否则累加值lineWidth,lineHeight并取最大高度
                    lineWidth += childWidth;
                    lineHeight = Math.max(lineHeight, childHeight);
                }

                if (child instanceof TextView){
                    Log.e(TAG, "onMeasure: "+((TextView)child).getText().toString());

                }

                //因为最后一行是不会超出width范围的，所以需要单独处理
                if (i == childCount - 1) {
                    width = Math.max(width, lineWidth);
                    height += lineHeight;
                }
            }
        }
        //当属性是MeasureSpec.EXACTLY时，那么他的高度就是确定的；
        //当属性是wrap_content时，由于是通过内部控件的大小来最终确定它的大小的，所以它的大小是不确定的，此时对应的属性是
        //MeasureSpec.AT_MOST,这就需要我们自己计算它应当的大小，并设置进去
        setMeasuredDimension((measureWidthMode == MeasureSpec.EXACTLY) ? measureWidth : width, (measureHeightMode == MeasureSpec.EXACTLY) ? measureHeight : height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int count = getChildCount();
        int lineWidth = 0;//累加当前行的行宽
        int lineHeight = 0;//当前行的行高
        int top = 0, left = 0;//当前控件的top坐标和left坐标
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            if (child.getLayoutParams() instanceof MarginLayoutParams) {
                MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
                int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
                int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
                if (lineWidth + childWidth > getMeasuredWidth()) {
                    //如果换行
                    top += lineHeight;
                    left = 0;
                    lineHeight = childHeight;
                    lineWidth = childWidth;

                } else {
                    lineHeight = Math.max(lineHeight, childHeight);
                    lineWidth += childWidth;
                }
                //计算childView 的left,top,right,bottom
                int lc = left + lp.leftMargin;
                int tc = top + lp.topMargin;
                int rc = lc + child.getMeasuredWidth();
                int bc = tc + child.getMeasuredHeight();
                child.layout(lc, tc, rc, bc);
                //将left置为下一个子控件的起始点
                left += childWidth;

            }
        }


    }

    @Override
    protected LayoutParams generateLayoutParams(LayoutParams p) {
        return new MarginLayoutParams(p);
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new MarginLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    }
}
